<template>
  <el-dialog v-model="visible" :title="!dataForm.restaurantId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="餐厅名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="餐厅名称"></el-input>
      </el-form-item>
      <el-form-item label="餐厅描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="餐厅描述"></el-input>
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="餐厅地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="餐厅地址"></el-input>
      </el-form-item>
      <el-form-item label="营业时间" prop="operatingHours">
        <el-date-picker type="datetime" placeholder="营业时间" v-model="dataForm.operatingHours" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="餐厅评分" prop="rating">
        <el-input v-model="dataForm.rating" placeholder="餐厅评分"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  restaurantId: "",
  name: "",
  description: "",
  latitude: "",
  longitude: "",
  address: "",
  operatingHours: "",
  rating: "",
  createdAt: "",
  updatedAt: "",
});

const rules = ref({
});

const init = (restaurantId?: number) => {
  visible.value = true;
  dataForm.restaurantId = restaurantId;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (restaurantId) {
    getInfo(restaurantId);
  }
};

// 获取信息
const getInfo = (restaurantId: number) => {
  baseService.get("/sys/restaurants/" + restaurantId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.restaurantId ? baseService.post : baseService.put)("/sys/restaurants", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>